import pygame
import random


class Tetris():
    def __init__(self):
        # 设置游戏初始化
        pygame.init()
        # 设置最小尺寸
        self.size = 30
        # 设置游戏区域宽度数量
        self.width = 10
        # 设置高度数量
        self.height = 20
        # 是否循环
        self.is_loop = True
        # 设置背景颜色
        self.bg_color = (189, 183, 107)
        # 设置网格线颜色
        self.line_color = (154, 205, 50)
        self.block_color = (255, 69, 0)
        # 设置字体和字号
        self.font1 = pygame.font.SysFont('Simhei', 18)
        # 设置字体颜色
        self.font_color = (255, 106, 106)
        # 等级关卡
        self.level = 1
        # 得分
        self.score = 0
        #  blocks记录方块形状。
        self.blocks = []
        # 当前方块形状
        self.cur_block = None
        # 下一个的方块形状r
        self.next_block = None
        #第一次出现的(x,y)的位置
        self.cur_block_lt = [4, -2]
        # 下一个方块形状的位置
        self.next_block_lt = [11, 7]


    def start(self):
        # 设置窗口大小
        self.screen = pygame.display.set_mode((self.size*(self.width+5), self.size*self.height))
        # 设置标题
        pygame.display.set_caption('俄罗斯方块')
        self.read_file()
        self.cur_block = self.get_block()
        self.next_block = self.get_block()
        clock = pygame.time.Clock()

        while self.is_loop:
            self.screen.fill(self.bg_color)
            self.event_handler()
            self.draw_grid()
            self.draw_text()
            self.draw_block(self.cur_block, self.cur_block_lt)
            self.draw_block(self.next_block, self.next_block_lt)
            self.move()
            clock.tick(3)
            # 更新屏幕
            pygame.display.update()
        self.quit()

    def event_handler(self):
        """事件处理助手"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.is_loop = False

    def draw_grid(self):
        """绘制游戏区域网格线"""
        for i in range(self.height+1):
            # 绘制线段
            pygame.draw.line(self.screen, self.line_color, (1, i*self.size), (self.width*self.size, i*self.size),2)
        for i in range(self.width+1):
            # 绘制线段
            pygame.draw.line(self.screen, self.line_color, (i*self.size, 0), (i*self.size, self.height*self.size), 2)

    def draw_text(self):
        """
        绘制文本信息，如关卡得分，下一个
        :return:
        """
        level_img = self.font1.render(
            f'关卡: {self.level}', True , self.font_color )
        self.screen.blit(level_img, (self.size*(self.width+1), self.size*2))
        score_img = self.font1.render(
            f'得分: {self.score}', True, self.font_color)
        self.screen.blit(score_img, (self.size * (self.width + 1), self.size * 4))

        next_img = self.font1.render(
            f'下一个:', True, self.font_color)
        self.screen.blit(next_img, (self.size * (self.width + 1), self.size * 6))


    def read_file(self):
        """读取配置文件"""
        with open('./elsfk.cfg', 'r') as file:
            self.blocks = file.readlines()

    def get_block(self):
        """获取一个方块形状"""
        return random.choice(self.blocks).strip()

    def draw_block(self, block, pos):
        """
        绘制方块形状，将所有是1的元素绘制小的矩形
        :param block: 方块形状
        :param pos:  位置
        :return:
        """
        x, y = pos
        for i, v in enumerate(block):
            if v == '1':
                # 或者矩形区域
                pygame.draw.rect(self.screen, self.block_color, ((x+i % 5)*self.size, (y+i//5)*self.size ,self.size, self.size))

    def move(self):
        """block方块形状移动"""
        x, y = self.cur_block_lt

        x_list, y_list = [], []
        for i, v in enumerate(self.cur_block):
            if v == '1':
                x_list.append(x+i % 5)
                y_list.append(y+i//5)

        max_x, max_y = max(x_list), max(y_list)
        if max_y==self.height-1:
            pass
        else:
            self.cur_block_lt[1] += 1




    def quit(self):
        """退出游戏"""
        pygame.display.quit()


if __name__ == '__main__':
    t = Tetris()
    t.start()
